package com.example.delicioushunter.view.activity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.example.delicioushunter.R;
import com.example.delicioushunter.control.supportcontrol.SDKUtils;
import com.example.delicioushunter.control.supportcontrol.DHSharedPreference;
import com.example.delicioushunter.model.database.DHDatabaseHelper;
import com.samsung.android.sdk.SsdkUnsupportedException;
import com.samsung.android.sdk.pen.Spen;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.Toast;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class MainActivity extends Activity implements OnClickListener,
		OnTouchListener {

	ImageView imgUser, imgSetting, imgHome, imgBook;

	String images[] = { "ic_background01", "ic_background02",
			"ic_background03", "ic_background04", "ic_background05",
			"ic_background06", "ic_background07" };
	int id[] = { R.drawable.ic_background01, R.drawable.ic_background02,
			R.drawable.ic_background03, R.drawable.ic_background04,
			R.drawable.ic_background05, R.drawable.ic_background06,
			R.drawable.ic_background07 };

	private boolean isOK = false;
	private Context mContext;
	private DHSharedPreference shared;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.activity_main);

		imgUser = (ImageView) findViewById(R.id.imgUser);
		imgSetting = (ImageView) findViewById(R.id.imgSetting);
		imgHome = (ImageView) findViewById(R.id.imgHome);
		imgBook = (ImageView) findViewById(R.id.imgBook);

		imgUser.setOnTouchListener(this);
		imgUser.setOnClickListener(this);
		imgSetting.setOnTouchListener(this);
		imgSetting.setOnClickListener(this);
		imgHome.setOnTouchListener(this);
		imgHome.setOnClickListener(this);
		imgBook.setOnTouchListener(this);
		imgBook.setOnClickListener(this);

		BuidDataTask task = new BuidDataTask();
		task.execute();

		shared = new DHSharedPreference(getApplicationContext());
		shared.setLogin(false);
		if (!shared.getSignature())
			isOK = true;
		if (shared.getLogin() && shared.getSignature())
			isOK = true;

		boolean isSpenFeatureEnabled = false;
		Spen spenPackage = new Spen();
		try {
			spenPackage.initialize(mContext);
			isSpenFeatureEnabled = spenPackage
					.isFeatureEnabled(Spen.DEVICE_PEN);
		} catch (SsdkUnsupportedException i) {
			if (SDKUtils.processUnsupportedException(this, i) == true) {
				return;
			}
		} catch (Exception i) {
			i.printStackTrace();
			finish();
		}

		shared.setIsSpenFeatureEnabled(isSpenFeatureEnabled);

	}

	@Override
	public void onClick(View v) {
		Intent intent = null;
		if (isOK == false) {
			intent = new Intent(MainActivity.this,
					SignatureRegistrationActivity.class);
			intent.putExtra("ACTION", "Authentication");
			startActivity(intent);
			return;
		}
		switch (v.getId()) {
		case R.id.imgUser:
			if (shared.getSignature() == true) {
				intent = new Intent(MainActivity.this,
						SignatureRegistrationActivity.class);
				intent.putExtra("ACTION", "Authentication");
				startActivity(intent);
			} else
				Toast.makeText(mContext, "You don't sign up any signature",
						Toast.LENGTH_SHORT).show();
			break;
		case R.id.imgSetting:
			intent = new Intent(MainActivity.this, UserSettingActivity.class);
			startActivity(intent);
			break;
		case R.id.imgHome:
			intent = new Intent(MainActivity.this, StorageActivity.class);
			startActivity(intent);
			break;
		case R.id.imgBook:
			intent = new Intent(MainActivity.this, BookActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			v.setBackgroundResource(R.drawable.border_picture_touched);
		} else if (event.getAction() == MotionEvent.ACTION_UP) {
			v.setBackgroundResource(R.drawable.border_picture);
		}
		return false;
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (!shared.getSignature())
			isOK = true;
		if (shared.getLogin() && shared.getSignature())
			isOK = true;
	}

	private class BuidDataTask extends AsyncTask<Void, Integer, Void> {

		@Override
		protected Void doInBackground(Void... params) {
			DHDatabaseHelper helper = new DHDatabaseHelper(MainActivity.this);
			SQLiteDatabase db = helper.getReadableDatabase();

			Bitmap bitmap;
			String fname;
			File file;
			String root = getApplicationContext().getExternalCacheDir()
					.toString();
			Log.d("Size", root);
			File myDir = new File(root + "/Delicious Hunter/Background");
			myDir.mkdirs();

			for (int i = 0; i < images.length; i++) {
				try {
					publishProgress(i);
					fname = images[i] + ".jpg";
					file = new File(myDir, fname);
					if (!file.exists()) {
						bitmap = BitmapFactory.decodeResource(getResources(),
								id[i]);

						FileOutputStream out = new FileOutputStream(file);
						bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
						out.flush();
						out.close();
						bitmap.recycle();
						bitmap = null;
					}
				} catch (IOException ex) {
					return null;
				}
			}
			return null;
		}
	}
}